<template>
	<view class="content">
		<view class="row b-b">
			<text class="tit">联系人</text>
			<input class="input" type="text" v-model="addressData.ship_name" placeholder="收货人姓名" placeholder-class="placeholder" />
		</view>
		<view class="row b-b">
			<text class="tit">手机号</text>
			<input class="input" type="number" v-model="addressData.ship_mobile" placeholder="收货人手机号码" placeholder-class="placeholder" />
		</view>
		
		<view class="row b-b">
			<text class="tit">地址</text>
			<view class="uni-list-cell-db" style="flex: 1;">
				<view class="content">
				        <view class="content_list" @click="addressShow = true">
				            <view class="content_list_label" v-if="!addressData.address_detail">点击选择地址</view>
				            <view class="content_list_content">
				                {{addressData.address_detail}}
				                <pickerAddress v-model="addressShow" @confirm="addresspick" />
				            </view>
				        </view>
				</view>
			</view>
		</view>
		<view class="row b-b">
			<text class="tit">详细地</text>
			<input class="input" type="text" v-model="addressData.ship_addr" placeholder="xxx街道xxx小区" placeholder-class="placeholder" />
			<text class="yticon icon-shouhuodizhi" @click="chooseLocation"></text>
		</view>
		
		<view class="row default-row">
			<text class="tit">设为默认</text>
			<switch :checked="addressData.is_default" color="#fa436a" @change="switchChange" />
			<!-- <switch :checked="true" v-if="addressData.is_default == 1" color="#fa436a" @change="switchChange" />
			<switch :checked="false" v-else color="#fa436a" @change="switchChange" /> -->
			<view class="swBg" v-if="isAddOk"></view>
		</view>
		<button class="add-btn" @click="confirm">提交</button>
	</view>
</template>

<script>
	import app from '../../../Json.js'
	import {mapState} from 'vuex';
	import pickerAddress from '@/components/liudx-pickerAddress/index.vue'
	export default {
		data() {
			return {
				addressShow: false,
				isUpdate:false,
				isAddOk:true,
				form: {
					province: '',
					city: '',
					area: '',
				},
				title: 'picker',
				addressData: {
					ship_name: '',
					ship_mobile: '',
					addressName: '在地图选择',
					area: '',
					ship_addr: '',
					address_detail:'',
					default: false,
					location_site:'',
					location_name:'',
					lat:'',
					lng:''
				},
				province:'',
				city:'',
				area:''
			}
		},
		onShow() {},
		onLoad(option){
			let title = '新增收货地址';
			if(option.type==='edit'){
				this.isUpdate = true;
				title = '编辑收货地址'
				this.addressData = JSON.parse(option.data)
			}
			this.manageType = option.type;
			uni.setNavigationBarTitle({
				title
			})
		},
		computed: {
			...mapState(['hasLogin','userInfo'])
		},
		components: {
		     pickerAddress
		},
		methods: {
			addresspick(obj){
				var that = this
				let arr = [ 'province', 'city', 'area'];
				let place = '';
				arr.map(key => {
				    this.form[key] = obj[key].region_id
				    place += obj[key].region_name
				})
				this.addressData.address_detail = place
			},
			// 地址
			switchChange(e){
				this.addressData.is_default = e.detail.value;
				this.isAddOk = !this.isAddOk
			},
			//地图选择地址
			chooseLocation(){
				var that = this
				uni.chooseLocation({
					latitude:that.addressData.lat,
					longitude:that.addressData.lng,
					success: (data)=> {
						that.addressData.location_site = data.address
						that.addressData.location_name = data.name
						that.addressData.lat = data.latitude
						that.addressData.lng = data.longitude
					}
				})
			},
			//提交
			confirm(){
				let data = this.addressData;
				if(!data.ship_name){
					this.$api.msg('请填写收货人姓名');
					return;
				}
				if(!/(^1[3|4|5|7|8][0-9]{9}$)/.test(data.ship_mobile)){
					this.$api.msg('请输入正确的手机号码');
					return;
				}
				if(!data.ship_addr){
					this.$api.msg('请输入详细地址');
					return;
				}
				if(!data.address_detail){
					this.$api.msg('请选择地址');
					return;
				}
				this.$set(data,"token",this.userInfo.token);
				this.$set(data,"ship_county_id",this.form.area || this.addressData.ship_county_id);
				this.$set(data,"ship_city_id",this.form.city|| this.addressData.ship_city_id);
				this.$set(data,"ship_province_id",this.form.province|| this.addressData.ship_province_id);
				//this.$set(data,"form",this.form);
				let that = this;
				this.$api.loading(true,"提交中");
				let _url = this.$api._api.ApiUserCreateAddress
				if(this.manageType =='edit'){
					_url = this.$api._api.ApiUserEditAddress
				}
				this.$api.postUrl(_url, data ,function(res){
					that.$api.prePage().refreshList(res,that.manageType)
					that.$api.msg(`地址${that.manageType=='edit' ? '修改': '添加'}成功`);
					setTimeout(()=>{
						uni.navigateBack({
							delta:1
						})
					}, 800)
				})
			},
		}
	}
</script>

<style lang="scss">
	page{
		background: $page-color-base;
		padding-top: 16rpx;
	}
	.row{
		display: flex;
		align-items: center;
		position: relative;
		padding:40rpx 30rpx;
		background: #fff;
		.tit{
			flex-shrink: 0;
			width: 120rpx;
			font-size: 30rpx;
			color: $font-color-dark;
		}
		.input{
			flex: 1;
			font-size: 30rpx;
			color: $font-color-dark;
		}
		.icon-shouhuodizhi{
			font-size: 52rpx;
			color: $font-color-light;
		}
	}
	.default-row{
		margin-top: 16rpx;position: relative;
		.tit{
			flex: 1;
		}
		switch{
			transform: translateX(16rpx) scale(.9);
		}
		/* #ifdef APP-PLUS */
		.swBg{
			position: absolute;right: 14px;top: 28px;width: 15px;height: 15px;
			border-radius: 50%;
			background:#dfdfdf;
		}
		/* #endif */
	}
	.add-btn{
		display: flex;
		align-items: center;
		justify-content: center;
		width: 690rpx;
		height: 80rpx;
		margin: 60rpx auto;
		font-size: $font-lg;
		color: #fff;
		background-color: $base-color;
		border-radius: 10rpx;
		box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4);
	}
</style>
